package com.zking.ssm.DYM.controller;

import com.zking.ssm.DYM.model.HotelMemberLevel;
import com.zking.ssm.DYM.model.HotelRoom;
import com.zking.ssm.DYM.service.IHotelMemberLevelService;
import com.zking.ssm.util.JsonData;
import com.zking.ssm.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @Author XXXXX
 * @Date 2021/01/23 22:34
 */
@Controller
@RequestMapping("/hotelMemberLevel")
public class HotelMemberLevelController {

    @Autowired
//    private HotelMemberLevel hotelMemberLevel;
    private IHotelMemberLevelService hotelMemberLevelService;

    @ModelAttribute
    public void init(Model model){
        System.out.println("init");
        HotelMemberLevel hotelMemberLevel = new HotelMemberLevel();
        model.addAttribute("hotelMemberLevel",hotelMemberLevel);
    }

    //添加数据
    @RequestMapping("/toAddHotelMemberLevel")
    public String toAddHotelMemberLevel(Model model) {
        System.out.println("toAddHotelMemberLevel");
        return "addHotelMemberLevel";
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(HotelMemberLevel hotelMemberLevel){
        System.out.println("add");
        hotelMemberLevelService.insert(hotelMemberLevel);
        return "redirect:toAddHotelMemberLevel";
    }

    //修改数据
    //① 更新酒店房间，先通过ID找到酒店房间
    @RequestMapping("/updateHotelMemberLevel/{id}")
    public String updateHotelMemberLevel(@PathVariable("id") Integer id, Model model){
        model.addAttribute("updateHotelRoom",hotelMemberLevelService.selectByPrimaryKey(id));
        return "updateHotelMemberLevel";
    }
    //② 然后修改，点击更新提交数据给后端
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(HotelMemberLevel hotelMemberLevel){
        hotelMemberLevelService.updateByPrimaryKey(hotelMemberLevel);
        return "redirect:updateHotelMemberLevel";
    }

    //删除酒店房间
    @RequestMapping(value = "/deleteHotelMemberLevelById")
    @ResponseBody
    public String deleteHotelMemberLevelById(Integer id){
        hotelMemberLevelService.deleteByPrimaryKey(id);
        System.out.println(id);
        return "redirect:HotelMemberLevel";
//        return "删除成功";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public JsonData list(HotelMemberLevel hotelMemberLevel, PageBean pageBean){
        System.out.println("list");
        List<HotelMemberLevel> hotelMemberLevelList = hotelMemberLevelService.list(hotelMemberLevel,pageBean);
//      System.out.println("集合:"+hotelRoomList);
        JsonData jsonData = new JsonData();
        jsonData.setCode(0);
        jsonData.setPage(pageBean.getPage());
        jsonData.setRows(pageBean.getRows());
        jsonData.setTotal(pageBean.getTotal());
        jsonData.setMessage("查询成功");
        jsonData.setResult(hotelMemberLevelList);
        return jsonData;
    }

    @RequestMapping(value = "/get")
    @ResponseBody
    public JsonData get(HotelMemberLevel hotelMemberLevel){
        HotelMemberLevel h = hotelMemberLevelService.selectByPrimaryKey(hotelMemberLevel.getId());
        JsonData jsonData = new JsonData();
        jsonData.setCode(0);
        jsonData.setMessage("查询成功");
        jsonData.setResult(h);
        return jsonData;
    }
}
